package cn.zc.service.impl;

import cn.zc.entity.*;
import cn.zc.mapper.AdminMapper;
import cn.zc.mapper.CourceMapper;
import cn.zc.mapper.StudentMapper;
import cn.zc.result.Result;
import cn.zc.service.UserService;
import cn.zc.utils.QiandaoUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private CourceMapper courceMapper;
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public List<CourceStudentVo> getCourcesByStudentId(String studentId) {
        List<CourceVo> courcesByStudentId = studentMapper.getCourcesByStudentId(studentId);
        ArrayList<CourceStudentVo> courceStudentVos = new ArrayList<>();
        for (CourceVo courceVo : courcesByStudentId) {
            CourceStudentVo courceStudentVo = new CourceStudentVo();
            courceStudentVo.setPeriod(courceVo.getPeriod());
            courceStudentVo.setDayOfWeek(courceVo.getDayOfWeek());
            courceStudentVo.setIsOver(courceVo.getIsOver());
            courceStudentVos.add(courceStudentVo);
//            if (courceVo.getIsOver()==0){
//                courceStudentVos.add(courceStudentVo);
//            }
        }
//        // 获取当前周数
//        int week = courceMapper.getWeek();
//        for (CourceVo courceVo : courcesByStudentId) {
//            CourceStudentVo courceStudentVo = new CourceStudentVo();
//            courceStudentVo.setPeriod(courceVo.getPeriod());
//            courceStudentVo.setDayOfWeek(courceVo.getDayOfWeek());
//            courceStudentVo.setIsOver(courceVo.getIsOver());
//            if(courceVo.getWeekPart() == 0){
//                List<Integer> arr=new ArrayList<>();
//                for (int i = courceVo.getStartWeek(); i <courceVo.getEndWeek()+1 ; i++) {
//                    arr.add(i);
//                }
//                courceStudentVo.setWeeks(arr);
//            } else if (courceVo.getWeekPart() == 1) {
//                List<Integer> arr=new ArrayList<>();
//                for (int i = courceVo.getStartWeek(); i <courceVo.getEndWeek()+1 ; i++) {
//                    if(i%2==1){
//                        arr.add(i);
//                    }
//                }
//                courceStudentVo.setWeeks(arr);
//            } else if (courceVo.getWeekPart() == 2) {
//                List<Integer> arr=new ArrayList<>();
//                for (int i = courceVo.getStartWeek(); i <courceVo.getEndWeek()+1 ; i++) {
//                    if(i%2==0){
//                        arr.add(i);
//                    }
//                }
//                courceStudentVo.setWeeks(arr);
//            }
//            if(courceStudentVo.getWeeks().contains(week)){
//                courceStudentVos.add(courceStudentVo);
//            }
//        }
        return courceStudentVos;
    }

    @Override
    public Result periodQiandao(Qiandao period) {
        studentMapper.periodQiandao(period);
        return Result.success();
    }

    @Override
    public boolean isTime(String period) {
        TimeVo oneTime = adminMapper.getOneTime(period);
        String endTime = oneTime.getStart_time();
//        String startTime = oneTime.getEnd_time();
        boolean between = QiandaoUtils.isBetween(endTime);
        return between;
    }

    @Override
    public List<LateTimeCource> overtime(String studentId) {
        String chidao="迟到";
        List<LateTimeCource> overtime = studentMapper.overtime(studentId,chidao);
//        String nameById = studentMapper.getNameById(studentId);
//        overtime.forEach(lateTimeCource -> {
//            lateTimeCource.setName(nameById);
//        });
        return overtime;
    }

    @Override
    public List<LateTimeCource> overtimeAll(int page, int size,String search,int type) {
        List<LateTimeCource> overtime = studentMapper.overtimeAll(size*(page-1), size,search,type);
        return overtime;
    }

    @Override
    public void buqian(LateTimeCource buqianvo) {
        studentMapper.buqian(buqianvo);
    }

    @Override
    public void setviolation(LateTimeCource buqianvo) {
        studentMapper.setviolation(buqianvo);
    }

    @Override
    public void editAdmin(User admin) {
        adminMapper.editAdmin(admin);
    }

    @Override
    public void addAdmin(User admin) {
        admin.setType(1);
        adminMapper.addAdmin(admin);
    }

    @Override
    @Transactional
    public void deleteStudent(String userid) {
        studentMapper.deleteViolation(userid);
        studentMapper.deleteCource(userid);
        studentMapper.deletelog(userid);
        studentMapper.deleteStudent(userid);
    }
}